<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="68px">
      <el-form-item label="" prop="editId">
        <el-input v-model="queryParams.editId" placeholder="编辑ID" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="" prop="userName">
        <el-input v-model="queryParams.userName" placeholder="请输入姓名" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="" prop="mobile">
        <el-input v-model="queryParams.mobile" placeholder="请输入手机号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="" prop="email">
        <el-input v-model="queryParams.email" placeholder="邮箱" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="" prop="status">
        <el-select v-model="queryParams.status" placeholder="账号状态" clearable>
          <el-option :label="'全部'" :value="''"></el-option>
          <el-option :label="'正常'" :value="0"></el-option>
          <el-option :label="'停用'" :value="1"></el-option>
        </el-select>
      </el-form-item>


      <el-form-item label="">
        <el-date-picker v-model="dateRange" style="width: 240px" value-format="yyyy-MM-dd" type="daterange"
          range-separator="-" start-placeholder="开始时间" end-placeholder="结束时间"></el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>

    </el-form>


    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport">导出</el-button>
      </el-col>


    </el-row>


    <el-table v-loading="loading" :data="tableList.slice((pageNum - 1) * pageSize, pageNum * pageSize)"
      style="width: 100%;" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="序号" type="index" align="center">
        <template slot-scope="scope">
          <span>{{(pageNum - 1) * pageSize + scope.$index + 1}}</span>
        </template>
</el-table-column> -->
      <el-table-column label="编辑ID" align="center" prop="editId" :show-overflow-tooltip="true" />
      <el-table-column label="姓名" align="center" prop="userName" :show-overflow-tooltip="true" />
      <el-table-column label="手机号" align="center" prop="mobile" />
      <el-table-column label="邮箱" align="center" prop="email" :show-overflow-tooltip="true" />
      <el-table-column label="本月处理字数" align="center" prop="monthProcessedWords" :show-overflow-tooltip="true" />
      <el-table-column label="累计处理字数" align="center" prop="totalProcessedWords" />

      <el-table-column label="账号状态" align="center">

        <template slot-scope="scope">
          <el-switch v-model="scope.row.status" active-value="0" inactive-value="1"
            @change="handleStatusChange(scope.row)"></el-switch>
        </template>
      </el-table-column>

      <el-table-column label="创建时间" align="center" prop="createTime" width="180">

        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="最后登陆" align="center" prop="loginTime" width="180">

        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.loginTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="登陆次数" align="center" prop="loginCount" />
      <el-table-column label="科藤备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">

        <template slot-scope="scope">
          <el-button type="text" size="small" @click="handleShowTable(scope.row)">查看详情</el-button>
        </template>
      </el-table-column>


    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="pageNum" :limit.sync="pageSize" />
  </div>
</template>

<script>
import { listTable, changeUserStatus } from "@/api/edit/edit";

export default {
  name: "edit",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 表格数据
      tableList: [],
      ids: [],
      pageNum: 1,
      pageSize: 10,
      // 日期范围
      dateRange: "",
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        editId: undefined,
        userName: undefined,
        mobile: undefined,
        email: undefined,
        time: undefined,
      },
      isSearch: false
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询登录日志列表 */
    getList() {
      let params = {}
      if (this.isSearch) {
        params = Object.assign({}, this.addDateRange(this.queryParams, this.dateRange))
      } else {
        params = {
          pageNum: this.queryParams.pageNum,
          pageSize: this.queryParams.pageSize
        }
      }
      this.loading = true;
      listTable(params).then(response => {
        this.tableList = response.rows;
        this.total = response.total;
        this.loading = false;
      }
      );
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.pageNum = 1;
      this.isSearch = true;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.isSearch = false;
      this.queryParams.pageNum = 1;
      this.getList();
    },
    // 任务状态修改
    handleStatusChange(row) {
      console.log(row);
      let text = row.status == 1 ? "停用":"启用";
      this.$modal.confirm('确认要' + text + ' ' + row.userName + ' 用戶吗？').then(function () {
        return changeUserStatus(row.editId, row.status);
      }).then(() => {
        this.$modal.msgSuccess(text + "成功");
      }).catch(function () {
        row.status = row.status === "0" ? "1" : "0";
      });
    },
    /** 修改按钮操作 */
    handleShowTable(row) {
      const tableId = row.editId || this.ids[0];
      const userName = row.userName || this.userName[0];
      const params = { pageNum: this.queryParams.pageNum };
      this.$tab.openPage("查看[" + userName + "]详情", '/edit/show-detail/index/' + tableId, params);
    },
    /** 导出按钮操作 */
    handleExport() {
      this.queryParams.ids = this.ids;
      this.download('edit/export', {
        ...this.queryParams,
        ids: this.ids.length? this.ids : undefined
      }, `编辑管理_${new Date().getTime()}.xlsx`)
    },
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.editId)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
  }

};
</script>
